<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Http\Controllers\HomeLoginController;
use Gregwar\Captcha\CaptchaBuilder;
use Carbon\Carbon;
use Session;
use DB;
use Hash;
use Mail;

class HomeRegisterController extends Controller
{
    public function getIndex()
    {
        //解析注册页面模板
        return view('home.register.register');
    }

    // 生成验证码
    public function getCode(){
        HomeLoginController::getCode();
    }

    public function postDoregister(Request $request)
    {
        // 接收所有数据
        $data = $request->all();

        // 验证用户是否存在
        if (!empty($data['username'])) {
            $res = DB::table('hm_users')->where('username', $data['username'])->first();
            if ($res) {
                echo 'error';
            }else{
                echo 'success';
            }
        }


        // 验证验证码是否正确
        if(!empty($data['authcode'])){
            if ($data['authcode'] == session('Vcode')) {
                return 'success';
            }else{
                return 'error';
            }
        }

        // 验证邮箱是否已被注册
        if (!empty($data['email'])) {
            $res = DB::table('hm_users')->where('email', $data['email'])->first();
            if ($res) {
                echo 'error';
            }else{
                echo 'success';
            }
        }
    }

    public function postInsert(request $request)
    {
        $data = $request->all();
        // dd($data);
        // 验证用户名称是否在4-20之间
        if (mb_strlen($data['uname']) <= 20 && mb_strlen($data['uname']) >=4){
            // 密码是否符合要求
            if (strlen($data['pwd']) <= 20 && mb_strlen($data['pwd']) >=6){
                // 确认密码和密码是否一致
                if ($data['repwd'] == $data['pwd']) {
                    // 正则判断邮箱和手机号格式是否正确
                    if (preg_match("/^1[3|4|5|7|8][0-9]{9}$/", $data['phone'])) {
                        if (preg_match("/^[A-Za-z0-9]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/", $data['email'])) {
                                // 插入数据
                                $data['addtime'] = Carbon::now()->toDateTimeString();
                                $pwd = Hash::make($data['pwd']);
                                $token = str_random(50);
                                $id = DB::table('hm_users')->insertGetId([
                                    'username' => $data['uname'],
                                    'password' => $pwd,
                                    'phone' => $data['phone'],
                                    'email' => $data['email'],
                                    'status' => '1',
                                    'token' => $token,
                                    'addtime' => $data['addtime']
                                ]);

                                if ($id) {
                                    // 添加session
                                    session(['id' => $id, 'username' => $data['uname']]); 
                                    // 发送邮件
                                    $email = $data['email'];
                                    Mail::send('home.email.email', ['id'=>$id,'token'=>$token], function ($message)use($email) {
                                        $message->to($email)->subject('激活邮件');
                                    });
                                    // 跳转注册成功页
                                    return redirect('/jihuo');
                                }else{
                                    return back()->with('error', '注册失败请联系管理员');
                                }
                        }    
                    }
                }
            }  
        }
    }

    public function getEmail(request $request){
        $data = $request->all();
        $token = DB::table('hm_users')->where('id', $data['id'])->value('token');
        // 判断用户token是否匹配
        if ($data['token'] == $token) {
            // 更新用户状态
            $res = DB::table('hm_users')->where('id', $data['id'])->update(['status' => 2]);
            if ($res) {
                 echo "<script>alert('恭喜您激活成功');window.location.href='http://lamp.cn';</script>";
            }else{
                echo "<script>alert('激活失败');window.location.href='http://lamp.cn';</script>";
            }
        }else{
            echo "<script>alert('您不是本站会员,请注册');window.location.href='http://lamp.cn';</script>";
        }   
    }

   
}
